/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.gui.widgets.visual;

import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;

import dgbgui.gui.shapes.RoundedRectangleShape;
import dgbgui.gui.widgets.Widget;
import edu.umd.cs.piccolo.PNode;
import edu.umd.cs.piccolo.util.PPaintContext;

@SuppressWarnings("serial")
public class RectangleWidget extends Widget {
    
    public RectangleWidget(PNode parent, Rectangle2D rect, double topRadius, double bottomRadius) {
        this(parent,rect.getX(),rect.getY(),rect.getWidth(),rect.getHeight(),topRadius,bottomRadius);
    }
    
    public RectangleWidget(PNode parent, Rectangle2D rect) {
        this(parent,rect,0,0);
    }
    
    public RectangleWidget(PNode parent,double x, double y, double width, double height) {
        this(parent,x,y,width,height,0,0);
    }
    
    public RectangleWidget(PNode parent,double x, double y, double width, double height, double topRadius, double bottomRadius) {
        super(parent);
        m_topRadius = topRadius;
        m_bottomRadius = bottomRadius;
        m_rectangle = new Rectangle2D.Double(x,y,width,height);
        setBounds(m_rectangle);
    }
    
    // This nodes uses an internal Rectangle2D to define its shape.
    public Rectangle2D getRectangle() {
        return m_rectangle;
    }

    // This method is important to override so that the geometry of 
    // the m_rectangle stays consistent with the bounds geometry.
    public boolean setBounds(double x, double y, double width, double height) {
        if(super.setBounds(x, y, width, height)) {
            m_rectangle.setFrame(x, y, width, height);
            return true;
        }
        return false;
    }

    // Non rectangular subclasses need to override this method so
    // that they will be picked correctly and will receive the
    // correct mouse events.
    public boolean intersects(Rectangle2D aBounds) {
        return m_rectangle.intersects(aBounds);
    }

    // Nodes that override the visual representation of their super
    // class need to override a paint method.
    public void paint(PPaintContext aPaintContext) {
        Graphics2D g2 = aPaintContext.getGraphics(); 
        
        Shape roundedShape = new RoundedRectangleShape(
                m_rectangle.getX(),m_rectangle.getY(),
                m_rectangle.getWidth(),m_rectangle.getHeight(),
                m_topRadius,m_bottomRadius);
        
        // Fill the shape
        g2.setPaint(getFillColour());
        g2.fill(roundedShape);
        // Draw the borders
        g2.setPaint(getStrokeColour());
        g2.setStroke(getStrokeStyle());
        g2.draw(roundedShape);

    }

    private double m_bottomRadius;
    private double m_topRadius;
    private Rectangle2D m_rectangle;

}
